package com.shop.www.controller;

import com.google.gson.Gson;
import com.shop.www.model.Cart;
import com.shop.www.model.Commodity;
import com.shop.www.model.User;
import com.shop.www.service.CartItemservice;
import com.shop.www.service.Commodityservice;
import com.shop.www.service.Orderservice;
import com.shop.www.service.Userservice;
import com.shop.www.task.myTimerTask;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.servlet.http.Part;
import java.io.IOException;
import java.util.List;
import java.util.Timer;

import static com.shop.www.ssm.util.StringUtil.isEmpty;

public class Commoditycontroller {
    private Commodityservice commodityservice;
    private Userservice userservice;
    private CartItemservice cartItemservice;
    private Orderservice orderservice;
    public String index(String key,HttpSession session){//首页上返回商家列表和商品列表
        if(isEmpty(key)){
            key="";}
        session.setAttribute("key",key);
        List commoditylist = commodityservice.getcommodityList(key);
        session.setAttribute("commodityiLsit",commoditylist);
        List sellerlist = commodityservice.getsellerList();
        session.setAttribute("sellerList",sellerlist);
        User user = (User) session.getAttribute("loginuser");
        Cart cart = cartItemservice.getCart(user);
        user.setCart(cart);
        session.setAttribute("loginuser",user);
        return ("index");
    }

    public String indexInfo(String key,HttpSession session){//首页上返回商品列表(Vue)
        if(isEmpty(key)){
            key="";}
        session.setAttribute("key",key);
        List commoditylist = commodityservice.getcommodityList(key);
        Gson gson = new Gson();
        String cartJsonStr = gson.toJson(commoditylist);
        return "json:"+cartJsonStr;
    }

    public String shop(Integer ID,HttpSession session){//店铺页面的上架商品
        User loginuser = (User) session.getAttribute("loginuser");
        List commoditylist = null;
        if(loginuser.getRole()==0){
             commoditylist = commodityservice.getcommodityListbyseller(ID,"%%");
        }else {
             commoditylist = commodityservice.getcommodityListbyseller(ID, "1");
        }
            session.setAttribute("commoditylist", commoditylist);
            User seller = userservice.getuserByID(ID);
            session.setAttribute("seller", seller);
        return ("shop");
    }

    public String seller(HttpSession session,String state){//店铺账号登录后的页面
        if(state==null){
            state="%%";
        }else {
            state = "%"+state+"%";
        }
        User user =(User)session.getAttribute("loginuser");
        List goodslist = commodityservice.getcommodityListbyseller(user.getUser_ID(),state);
        List shippedlist = orderservice.getorderListByseller(user.getUser_ID());
        session.setAttribute("shippedlist",shippedlist);
        session.setAttribute("goodslist",goodslist);
        session.setAttribute("state",state);
        return ("backstage");
    }

    public String addware(Integer commodity_ID,String state){//上下架
        commodityservice.updatecommoditystate(new Commodity(commodity_ID,state));
        return ("redirect:Commodity.do?operate=seller");
    }
    public String delayware(Integer commodity_ID,String state){//延时上下架
        Timer timer = new Timer();
        myTimerTask timerTask = new myTimerTask(commodity_ID,state);
        timer.schedule(timerTask,60*60*1000L);
        return ("redirect:Commodity.do?operate=seller");
    }

    //添加新商品
    public String addCommodity(String commodity_name, Double price, Integer inventory, Part part, HttpServletRequest request,HttpSession session) throws IOException {
        User user = (User)session.getAttribute("loginuser");
        String type = part.getContentType();
        String cd = part.getHeader("content-disposition");
        String fname = cd.substring(cd.lastIndexOf("filename")+10,cd.length()-1);
        Commodity commodity = new Commodity(0,fname,commodity_name,price,0,inventory,user.getUser_ID(),"0");
        commodityservice.addcommodity(commodity);
        String path = request.getServletContext().getRealPath("/");
        path = path+"\\static\\uploads\\";
         String path2 = "C:\\Users\\fxab\\Idea-Projects\\yidong\\web\\static\\uploads\\";
        part.write(path+fname);
        /*part.write(path2+fname);*/
        return ("redirect:Commodity.do?operate=seller");
    }

    public String updateCommodity(Integer commodity_ID,HttpSession session){
        Commodity commodity = commodityservice.getcommodityByID(commodity_ID);
        session.setAttribute("Tochangecommodity",commodity);
        return("updateCommodity");
}
    //删除商品
    public String delCommodity(Integer commodity_ID,HttpSession session){
        commodityservice.delcommodity(commodity_ID);
        return("redirect:Commodity.do?operate=seller");
    }

    //店家自行修改商品信息
    public String updateCommoditytwo(String commodity_name, Double price, Integer inventory, Part part, HttpServletRequest request,HttpSession session) throws IOException {
        Commodity commodity = (Commodity) session.getAttribute("Tochangecommodity");
        commodity.setCommodity_name(commodity_name);
        commodity.setPrice(price);
        commodity.setInventory(inventory);
        String type = part.getContentType();
        String cd = part.getHeader("content-disposition");
        String fname = cd.substring(cd.lastIndexOf("filename")+10,cd.length()-1);
        //如果商品处于封禁状态
        if("02".equals(commodity.getState())|| "04".equals(commodity.getState())){
            commodity.setState("03");
            commodityservice.updatecommoditystate(commodity);
        }
        //如果存入了图片
        if(fname.length()>1){
            commodity.setCommodity_IMG(fname);
            String path = request.getServletContext().getRealPath("/");
            path = path+"\\static\\uploads\\";
            part.write(path+fname);
        }
        commodityservice.updatecommodity(commodity);
        return ("redirect:Commodity.do?operate=seller");
    }

    public String bancommodity(Integer commodity_ID,String state){
        commodityservice.updatecommoditystate(new Commodity(commodity_ID,state));
        return ("redirect:User.do?operate=admin");
    }
}
